package com.guigu.user.controller;

import cn.hutool.core.util.RandomUtil;
import com.guigu.tms.result.R;
import com.guigu.tms.result.ResultCode;
import com.guigu.user.controller.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:UserController
 * @Description:模拟登录,切勿当真
 * @Author:taoLong
 * @Date:2021/11/9:18:11 星期二
 * @email:2391611102@qq.com or 2391611102qq@gmail.com
 */
@RestController
public class UserController {
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * TODO 模拟登录
     *
     * @return
     */
    @PostMapping("/user/login")
    public R<Map<String, String>> login() {
        Map<String, String> map = new HashMap<>();
        map.put("token", "admin-token");
        return new R(ResultCode.SUCCESS_CODE, "登录成功!", true, map);
    }

    /**
     * TODO 获取验证码
     *
     * @return
     */
    @GetMapping("/user/getCode/{phone}")
    public R getCode(@PathVariable(value = "phone") String phone) {
        String code = RandomUtil.randomNumbers(6);

        Boolean b = redisTemplate.opsForValue().setIfAbsent(phone, code, Duration.ofMinutes(5));
        return new R(ResultCode.SUCCESS_CODE, "验证码发送成功!", b);
    }

    /**
     * TODO 校验验证码
     *
     * @return
     */
    @GetMapping("/user/checkCode/{phone}/{code}")
    public R checkCode(@PathVariable("phone") String phone,@PathVariable("code") String code) {
        String o = (String) redisTemplate.opsForValue().get(phone);
        if(!o.equals(code)){
            return new R(ResultCode.ERROR_CODE, "验证码校验错误!", false);
        }

        return new R(ResultCode.SUCCESS_CODE, "验证成功!", true);
    }

    /**
     * TODO 模拟获取用户信息
     *
     * @param token
     * @return
     */
    @GetMapping("/user/info")
    public R<Map> info(@RequestParam String token) {
        Map<String, Object> map = new HashMap<>();
        map.put("roles", "admin");
        map.put("introduction", "I am a super administrator");
        map.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("name", "Super Admin");
        return new R(ResultCode.SUCCESS_CODE, "信息获取成功!", map);
    }


    /**
     * TODO 模拟获取信息
     *
     * @return
     */

    @GetMapping("/user/list")
    public R<Map> list() {
        Map<String, Object> map = new HashMap<>();
        map.put("total", "20");
        List<UserInfo> list = new ArrayList<>();
        list.add(new UserInfo("2EF6cDd6-1C6b-6d21-aA72-9a11E865A1ED", "1491629104002", "Steven Lopez", "3428.42", "pending"));
        list.add(new UserInfo("7c27DAF4-3f8d-d1Bb-138B-FeBe57C16fcb", "1491629104002", "Jeffrey Moore", "4537.2", "pending"));
        list.add(new UserInfo("DbAFfF3C-5ee2-0997-1e87-33AF70437c3E", "1491629104002", "Steven Lopez", "3428.42", "pending"));
        list.add(new UserInfo("37acED8A-688f-DFc8-bbDe-4EA1c5D31871", "1491629104002", "Steven Lopez", "3428.42", "pending"));
        list.add(new UserInfo("23948C87-9C42-31F7-ef9a-b5BF4Ed9e4b4", "1491629104002", "Steven Lopez", "3428.42", "pending"));
        map.put("items",list);
        return new R(ResultCode.SUCCESS_CODE, "信息获取成功!", map);
    }
}
